package com.google.code.irouter.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.code.irouter.model.Api;
import com.google.code.irouter.model.Model;

public final class ApiUtil {

	public static void init() {
		Map<String, Model> apiMap = new HashMap<String, Model>();
		List<Model> modelList = ModelUtil.select(Api.class);
		for(Model model : modelList) {
			Api api = (Api) model;
			String key = getKey(api.getType(), String.valueOf(api.getKey().getId()));
			apiMap.put(key, api);
		}
		
		CacheUtil.put(Api.class.getName(), apiMap);
	}

	public static Map<String, String> list(String type) {
		Map<String, String> result = new HashMap<String, String>();
		Map<String, Model> apiMap = getMaster();
		Set<String> keySet = apiMap.keySet();
		for(String key : keySet) {
			Api api = (Api) apiMap.get(key);
			if(api.getType().equals(type)) {
				result.put(String.valueOf(api.getKey().getId()), api.getName());
			}
		}
		return result;
	}

	public static void add(String type, String name, String refrence) {
		Api api = new Api();
		api.setType(type);
		api.setName(name);
		api.setRefrence(refrence);
		api.pushCreatDate();
		
		try {
			api = (Api) ModelUtil.save(api);			
			Map<String, Model> apiMap = getMaster();
			String key = getKey(api.getType(), String.valueOf(api.getKey().getId()));
			if(!apiMap.containsKey(key)) {
				apiMap.put(key, api);
				CacheUtil.put(Api.class.getName(), apiMap);
			}			
		} catch (Exception exception) {
			// TODO
		}
		
	}
	
	public static void remove() {
		Map<String, Model> apiMap = getMaster();
		Set<String> keySet = apiMap.keySet();
		for(String key : keySet) {
			remove(String.valueOf(apiMap.get(key).getKey().getId()));
		}
	}
	
	public static void update(String id, String name, String type, String refrence) {		
		try {			
			Key keyApi = KeyFactory.createKey(Api.class.getSimpleName(), Long.valueOf(id));
			Map<String, Model> apiMap = getMaster();
			List<Model> modelList = ModelUtil.select(Api.class, keyApi);
			if(modelList.size() > 0) {
				Api api = (Api) modelList.get(0);
				api.setName(name);
				api.setRefrence(refrence);
				api.setType(type);
				apiMap.remove(getKey(api.getType(), String.valueOf(api.getKey().getId())));
				CacheUtil.put(Api.class.getName(), apiMap);
				api.pushUpdateDate();
				ModelUtil.save(api);
				String key = getKey(api.getType(), String.valueOf(api.getKey().getId()));
				if(!apiMap.containsKey(key)) {
					apiMap.put(key, api);
					CacheUtil.put(Api.class.getName(), apiMap);
				}
			}
			
		} catch (Exception exception) {
			// TODO
		}
	}
	
	public static void remove(String id) {		
		try {
			Key key = KeyFactory.createKey(Api.class.getSimpleName(), Long.valueOf(id));
			Map<String, Model> apiMap = getMaster();
			List<Model> modelList = ModelUtil.select(Api.class, key);
			if(modelList.size() > 0) {
				Api api = (Api) modelList.get(0);
				apiMap.remove(getKey(api.getType(), String.valueOf(api.getKey().getId())));
				CacheUtil.put(Api.class.getName(), apiMap);
				ModelUtil.remove(api);
			}			
		} catch (Exception exception) {
			// TODO
		}
		
	}
	
	
	public static Api get(String type, String id) {		
		Map<String, Model> apiMap = getMaster();
		return (Api) apiMap.get(getKey(type, id));
	}
	
	private static String getKey(String type, String id) {
		return TextUtil.contact(
					true, 
					Api.class.getName(),
					type,
					id
				);
	}
	
	@SuppressWarnings("unchecked")
	private static Map<String, Model> getMaster() {
		if(!CacheUtil.containsKey(Api.class.getName())) {
			init();
		}
		return (Map<String, Model>) CacheUtil.get(Api.class.getName());
	}
}
